﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Application.SiteLibraryManage;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YunSpace.Domain.ViewModel.SiteLibraryManage;
using YunSpace.Web.Common;
#endregion

namespace YunSpace.Web.Areas.SiteManage.Controllers
{
    public class SpaceController : ControllerBase
    {
        private SpaceApp spaceApp = new SpaceApp();
        private MarketApp marketApp = new MarketApp();
        private PhotosApp photosApp = new PhotosApp();
        private intentionalityApp intentionalityApp = new intentionalityApp();

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult PriceSchedule()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult AddForm()
        {
            return View();
        }
        
       [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult Schedule()
        {
            return View();
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string queryJson)
        {
            var operatorProvider = OperatorProvider.Provider.GetCurrent();
            var data = new
            {
                rows = spaceApp.GetList(pagination, queryJson,operatorProvider.UserId),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridSpaceJson(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = spaceApp.GetSpaceList(pagination, queryJson),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = spaceApp.GetForm(keyValue);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 提交表单
        /// </summary>
        /// <param name="spaceEntity">表单实体</param>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateInput(false)]
        public ActionResult SubmitForm([ModelBinder(typeof(JsonBinder<SpaceActionManage>))]SpaceActionManage spaceOutput, string keyValue)
        {
            spaceOutput.spaceEntity.state = 5;
            spaceApp.SubmitForm(spaceOutput.spaceEntity,spaceOutput.PhotosList, spaceOutput.marketEntity, keyValue);
            //spaceOutput.marketEntity.F_SpaceId = spaceOutput.spaceEntity.F_Id;
            //marketApp.SubmitForm(spaceOutput.marketEntity, keyValue);

            return Success("操作成功。");
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetListBySiteId(string F_SiteId)
        {
            var data = spaceApp.GetListBySiteId(F_SiteId);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetListBySpaceId(string F_SpaceId)
        {
            var data = intentionalityApp.GetListBySpaceId(F_SpaceId);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult intentionalitySubmitForm(intentionalityEntity intentionalityEntity)
        {
            if (intentionalityEntity.F_Id.Length < 10)
            {
                intentionalityEntity.F_Id = "";
            }
            intentionalityApp.SubmitForm(intentionalityEntity, intentionalityEntity.F_Id);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult DeleteIntentionalityForm(string keyValue)
        {
            intentionalityApp.DeleteForm(keyValue);
            return Success("操作成功。");
        }
        

        /// <summary>
        /// 删除表单
        /// </summary>
        /// <param name="keyValue">表单主键</param>
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            spaceApp.DeleteForm(keyValue);

            return Success("删除成功。");
        }
    }
}


